import importlib.util
import os

# 获取当前文件所在目录
current_dir = os.path.dirname(os.path.abspath(__file__))

# 构建模型文件路径
model_000_path = os.path.join(current_dir, "000_KNN_Adaboost_GDBT.py")
model_001_path = os.path.join(current_dir, "001_XGB_LGBM.py")

def load_module_from_file(file_path, module_name):
    """从指定路径加载模块"""
    spec = importlib.util.spec_from_file_location(module_name, file_path)
    if spec is None:
        raise FileNotFoundError(f"WARNING:无法加载模块：{file_path}")
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    return module


def run_model(model_path, module_name):
    """运行指定模型"""
    try:
        model_module = load_module_from_file(model_path, module_name)
        if hasattr(model_module, 'main'):
            print(f"Successful:正在运行 {module_name} 模型...")
            model_module.main()  # 调用 main 函数
        else:
            print(f"Error:模块 {module_name} 没有 main 函数，无法运行。")
    except Exception as e:
        print(f"Error:加载模型失败：{e}")


if __name__ == '__main__':
    print("欢迎使用模型训练选择程序：")
    # 可选择运行的模型
    print("请选择要运行的模型：")
    print("1. KNN + AdaBoost + GradientBoosting 模型")
    print("2. XGBoost + LightGBM 模型")
    print("3. 运行所有模型(运行1和2)")

    choice = input("请输入选项 (1/2/3): ").strip()

    if choice == '1':
        run_model(model_000_path, "KNN_Adaboost_GDBT")
    elif choice == '2':
        run_model(model_001_path, "XGB_LGBM")
    elif choice == '3':
        run_model(model_000_path, "KNN_Adaboost_GDBT")
        run_model(model_001_path, "XGB_LGBM")
    else:
        print("Error:输入无效，请重新运行程序。")